package xyz.csmcool.imroom.mapper;

import org.apache.ibatis.annotations.*;
import xyz.csmcool.imroom.entity.UserFriend;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author csmcool
 * @since 2022-08-15
 */
@Mapper
public interface UserFriendMapper extends BaseMapper<UserFriend> {

    /**
     * 查找我的朋友
     * @param page
     * @param limit
     * @param id
     * @return
     */
    @Select("SELECT a.`user` FROM ( " +
            "SELECT friend AS `user`,create_time FROM `sjim_user_friend` WHERE `user` =#{id}  AND is_pass = 1 " +
            "UNION " +
            "SELECT `user`,create_time FROM `sjim_user_friend` WHERE friend=#{id} AND is_pass = 1 " +
            ") a ORDER BY a.create_time DESC LIMIT #{page},#{limit}")
    List<Integer> selectMyFriend(@Param("page") Long page, @Param("limit") Long limit, @Param("id") Integer id);

    /**
     * select count
     * @param id
     * @return
     */
    @Select("SELECT SUM(sum1) FROM ( " +
            "SELECT COUNT(*) AS sum1 FROM `sjim_user_friend` WHERE `user` =#{id} AND is_pass = 1 " +
            "UNION " +
            "SELECT COUNT(*) AS sum1 FROM `sjim_user_friend` WHERE friend =#{id}  AND is_pass = 1 " +
            ") AS a")
    Integer selectMyFriendCount(@Param("id") Integer id);

    /**
     * selectAllMyFriendId
     * @param id
     * @return
     */
    @Select("SELECT friend `user` FROM `sjim_user_friend` WHERE `user`=#{id} AND is_pass = 1 " +
            "UNION " +
            "SELECT `user` FROM `sjim_user_friend` WHERE `friend`=#{id} AND is_pass = 1")
    List<Integer> selectAllMyFriendId(@Param("id") Integer id);

    /**
     * 是否是朋友*
     * @param uid
     * @param fid
     * @return
     */
    @Select("SELECT SUM(sum1) FROM ( " +
            "SELECT COUNT(*) AS sum1 FROM `sjim_user_friend` WHERE `user` =#{uid} AND friend=#{fid} AND is_pass = 1 " +
            "UNION " +
            "SELECT COUNT(*) AS sum1 FROM `sjim_user_friend` WHERE `user` =#{fid} AND friend=#{uid} AND is_pass = 1 " +
            ") AS a")
    Integer selectUserFriend(@Param("uid") Integer uid,@Param("fid") Integer fid);

    /**
     * 是否存在记录
     * @param uid
     * @param fid
     * @return
     */
    @Select("SELECT is_pass FROM sjim_user_friend " +
            "WHERE (`user` = #{uid} AND friend = #{fid}) OR (`user` = #{fid} AND friend = #{uid}) LIMIT 1")
    Integer selectUserIsPass(@Param("uid") Integer uid,@Param("fid") Integer fid);

    /**
     * 添加新朋友
     * @param uid
     * @param fid
     * @param pass
     * @return
     */
    @Insert("INSERT INTO `sjim`.`sjim_user_friend` " +
            "(`user`, `friend`, `is_pass`) VALUES (#{uid}, #{fid},#{pass}) " +
            "ON DUPLICATE KEY UPDATE is_pass=#{pass}")
    Boolean addOrUpdateFriend(@Param("uid") Integer uid,@Param("fid") Integer fid,
                              @Param("pass") Integer pass);


    /**
     * 查看添加我做朋友
     * @param page
     * @param limit
     * @param uid
     * @return
     */
    @Select("SELECT `user`,is_pass FROM sjim_user_friend WHERE friend = #{uid} ORDER BY create_time DESC LIMIT #{page},#{limit}")
    List<UserFriend> selectAddMe(@Param("page") Long page, @Param("limit") Long limit,
                                 @Param("uid") Integer uid);

    /**
     * 查找我的朋友
     * @param uid
     * @return
     */
    @Select("SELECT COUNT(*) FROM sjim_user_friend WHERE friend = #{uid}")
    Integer selectAddMeCount(@Param("uid") Integer uid);

    /**
     * 添加新朋友
     * @param uid
     * @param fid
     * @return
     */
    @Update("UPDATE sjim_user_friend SET " +
            "`user` = #{uid},friend = #{fid},is_pass=0 WHERE " +
            "(`user` = #{uid} AND friend = #{fid}) OR (`user` = #{fid} AND friend = #{uid})")
    Boolean addNewFriend(@Param("uid") Integer uid,@Param("fid") Integer fid);
}
